package ch.wanandroid.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.support.v7.widget.Toolbar;

import butterknife.BindView;
import ch.wanandroid.comr.wanandroid.R;
import ch.wanandroid.lib.ui.base.BaseActivity;

/**
 * Author: ch
 * Date: 2018/10/23 10:47
 * Description: ${DESCRIPTION}
 */

public class WebViewActivity extends BaseActivity {

    @BindView(R.id.web_view)
    WebView mWebView;
    @BindView(R.id.progres_bar)
    ProgressBar mProgressBar;
    @BindView(R.id.tool_bar)
    Toolbar mToolbar;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initView() {
        super.initView();
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
        }
        mToolbar.setNavigationIcon(R.drawable.ic_arrow_back);
    }

    @Override
    protected void initData() {
        mWebView.loadUrl("http://www.wanandroid.com/wxarticle/list/408/1");
        mWebView.setWebChromeClient(new WebChromeClient(){

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress < 100) {
                    mProgressBar.setProgress(newProgress);
                    mToolbar.setTitle("正在加载中");
                } else {
                    mProgressBar.setVisibility(View.GONE);
                    mToolbar.setTitle("成功");
                }
            };

        });
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
    }
}
